import wInput from '../../components/watch-login/watch-input.vue' //input
import wButton from '../../components/watch-login/watch-button.vue' //button
import * as login from '@/api/login.js'
import * as index from '@/api/index.js'
export default {
	data() {
		return {
			wxOpenId: '', //获取的微信code
			//logo图片 base64
			logoImage: '',
			logo: '',
			phoneData: '', //用户/电话
			verCode: '', //验证码
			isRotate: false, //是否加载旋转
			showAgree: false, //协议是否选择
			isFocus: true, // 是否聚焦
			wx_login: {},
			imUsername: '',
			imPassword: '123456',
			im_register: '',
			nickname: '',
			pnickname: '',
			// Type: '',
		};
	},
	components: {
		wInput,
		wButton,
	},
	onLoad() {
		// this.Type = uni.getStorage('Type')
		//获取openid
		// #ifndef APP-PLUS
		this.getOpenId()
		// #endif
		this.onLoadData();
	},
	methods: {
		getOpenId() {
			uni.login({
				success: res => {
					this.wxOpenId = res.code
				}
			})
		},
		isShowAgree() {
			//是否选择协议
			this.showAgree = !this.showAgree;
		},
		onLoadData() {
			index.nameGetConfig({
				name: 'wx_logo'
			}).then(res => {
				this.logoImage = res.data.wx_logo
			})
		},
		startLogin(e) {
			var that = this
			console.log(e)
			//登录
			if (this.isRotate) {
				//判断是否加载中，避免重复点击请求
				return false;
			}
			if (this.showAgree == false) {
				uni.showToast({
					icon: 'none',
					position: 'bottom',
					title: '请先同意《协议》'
				});
				return false;
			}
			if (this.phoneData.length == "") {
				uni.showToast({
					icon: 'none',
					position: 'bottom',
					title: '用户名不能为空'
				});
				return;
			}
			if (this.verCode.length != 6) {
				uni.showToast({
					icon: 'none',
					position: 'bottom',
					title: '验证码不正确'
				});
				return false;
			}

			this.isRotate = true
			setTimeout(() => {
				this.isRotate = false
			}, 1000)
			var data = {
				code: this.verCode,
				phone: this.phoneData,
				//#ifdef MP-WEIXIN
				type: 0,
				//#endif
				//#ifdef APP-PLUS
				type: 1,
				//#endif
			}
			console.log(data)
			login.memberPhoneLogin(data).then(res => {
				var Token = res.data.token
				//#ifdef APP-PLUS
				plus.push.getClientInfoAsync((info) => {
					let cid = info["clientid"];
					console.log(cid);
					index.memberBdCid({
						cid: cid
					}).then(res => {
						console.log('调用', res);
					})
				});
				//#endif  
				uni.setStorage({
					key: 'token',
					data: res.data.token,
					success: res => {
						// #ifndef APP-PLUS
						login.MemberdbWxOpenid({
							code: this.wxOpenId
						}).then(res => {
							this.$tools.navigate('/pages/home/index', true)
						})
						// #endif
						// #ifdef APP-PLUS
						this.$tools.navigate('/pages/home/index', true)
						// #endif
					}
				});


			})
		},

		// },
		// disconnect() { //断开链接
		// 	if(this.goEasy.getConnectionStatus() != 'disconnected'){
		// 		this.goEasy.disconnect({
		// 			onSuccess: function() {
		// 				console.log('断开成功')
		// 				uni.removeStorageSync('currentUser');
		// 			},
		// 			onFailed: function(error) {
		// 				// 务必确保断开成功才允许客户退出应用，否则有可能会收到上个用户的消息。

		// 				console.log('断开失败', error);

		// 			}
		// 		});
		// 	}

		// },
		// // 获取用户信息
		// getMember() {
		// 	var that = this
		// 	// this.getImage()
		// 	getJgReg().then(res => {

		// 		this.imUsername = res.member_id,
		// 			this.im_register = res.im_register
		// 		this.nickname = res.member.name
		// 		this.pnickname = res.company.name
		// 		this.avatar = res.company.avatar
		// 		// this.getImage(res.company.head)
		// 		// this.$jjim.config(this.imUsername, this.imPassword, this.nickname, this.pnickname);
		// 		this.JJIMLogin(this.imUsername, this.imPassword, this.nickname, this.avatar)



		//下载图片
		getImage(url) {
			let _this = this
			uni.saveImageToPhotosAlbum({
				filePath: url,
				success: (res) => {
					console.log(res, '图片')
				},
				fail: () => {
					_this.toasttxt = '图片保存失败';
				},
				complete: () => {}
			});
		},
		//goeazy
		getIdentity() {

		},
		// 查看用户协议
		agreementBnt() {
			this.$tools.navigate('/pages/login/agreement/index?name=wx_protocol')
		},
		yinsitBnt() {
			this.$tools.navigate('/pages/login/agreement/index?name=yinsixieyi')
		},
		getVerCode() {
			//获取验证码
			if (this.phoneData.length != 11) {
				uni.showToast({
					icon: 'none',
					position: 'bottom',
					title: '手机号不正确'
				});
				return false;
			}
			login.phoneGetCode({
				phone: this.phoneData
			}).then(res => {
				this.$refs.runCode.$emit('runCode'); //触发倒计时
			})
		},
	}
}
